package com.yd.happyspine.ui.main.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.jaydenxiao.common.base.BaseActivity;
import com.yd.happyspine.R;
import com.yd.happyspine.been.TabEntity;
import com.yd.happyspine.ui.main.fragment.PersonFragment;
import com.yd.happyspine.ui.main.fragment.PracticeFragment;
import com.yd.happyspine.ui.main.fragment.TeachingFragment;

import java.util.ArrayList;

import butterknife.Bind;

public class HomeActivity extends BaseActivity {


    @Bind(R.id.ct_TabLayout)
    CommonTabLayout ctTabLayout;
    private int[] IconUnSelectIds = { R.mipmap.cn_tab_practice_normal, R.mipmap.cn_tab_teaching_normal,R.mipmap.cn_tab_person_normal};
    private int[] IconSelectIds = {R.mipmap.cn_tab_practice_press, R.mipmap.cn_tab_teaching_press,R.mipmap.cn_tab_person_press};
    private PersonFragment personFragment;
    private PracticeFragment practiceFragment;
    private TeachingFragment teachingFragment;
    private String[] mTitles;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    public void initPresenter() {
    }

    @Override
    public void initView() {
        mTitles = new String[]{getString(R.string.str_tabPractice),getString(R.string.str_tabTeach),getString(R.string.str_tabPerson) };
        initTab();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initFragment(savedInstanceState);
    }

    private void initTab() {
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], IconSelectIds[i], IconUnSelectIds[i]));
        }
        ctTabLayout.setTabData(mTabEntities);
        //点击监听
        ctTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                SwitchTo(position);
            }
            @Override
            public void onTabReselect(int position) {
            }
        });
    }

    private void initFragment(Bundle savedInstanceState) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        int currentTabPosition = 0;
        if (savedInstanceState != null) {
            practiceFragment = (PracticeFragment) getSupportFragmentManager().findFragmentByTag("practiceFragment");
            teachingFragment = (TeachingFragment) getSupportFragmentManager().findFragmentByTag("teachingFragment");
            personFragment = (PersonFragment) getSupportFragmentManager().findFragmentByTag("personFragment");
        } else {
            personFragment = new PersonFragment();
            practiceFragment = new PracticeFragment();
            teachingFragment = new TeachingFragment();

            fragmentTransaction.add(R.id.fl_homeContent, practiceFragment, "practiceFragment");
            fragmentTransaction.add(R.id.fl_homeContent, teachingFragment, "teachingFragment");
            fragmentTransaction.add(R.id.fl_homeContent, personFragment, "personFragment");
        }
        fragmentTransaction.commit();
        SwitchTo(currentTabPosition);
        ctTabLayout.setCurrentTab(currentTabPosition);
    }

    private void SwitchTo(int currentTabPosition) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        switch (currentTabPosition) {
            case 0:
                fragmentTransaction.hide(teachingFragment);
                fragmentTransaction.hide(personFragment);
                fragmentTransaction.show(practiceFragment);
                fragmentTransaction.commitAllowingStateLoss();
                break;
            case 1:
                fragmentTransaction.hide(practiceFragment);
                fragmentTransaction.hide(personFragment);
                fragmentTransaction.show(teachingFragment);
                fragmentTransaction.commitAllowingStateLoss();
                break;
            case 2:
                fragmentTransaction.hide(teachingFragment);
                fragmentTransaction.hide(teachingFragment);
                fragmentTransaction.show(personFragment);
                fragmentTransaction.commitAllowingStateLoss();
                break;
            default:
                break;
        }
    }
}
